package com.yay.euro;

import java.util.List;

import com.yay.euro.model.LFType;
import com.yay.euro.model.Ticket;
import com.yay.euro.service.L10N;
import com.yay.euro.service.TicketsService;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.Resources;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;


/**
 * Get Started!
 * @author yakovlev.yuriy@gmail.com
 * @author Dmitry Makeev
 */
public class Dashboard extends Activity implements OnClickListener {
    public static String SERVER_URL;
    private LinearLayout layout_home;
    private Animation animation_fade_in;
    private Animation animation_fade_out;
    private static TicketsService ticketsService;
    private ImageView lost;
    private ImageView found;
	private TextView lblLost;
    private TextView lblFound;
    public static boolean reload;
    
    private TicketsService getTicketsService() {
		//if(ticketsService == null) {
			ticketsService = new TicketsService();
		//}
		return ticketsService;
	}
	
    private void loadPreferences() {
    	if(SERVER_URL == null) {
	    	SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
	        SERVER_URL = prefs.getString(this.getString(R.string.serverUrl), "http://getstarted.com.ua/euro/");
    	}
    }
    
    private LinearLayout getLayoutHome() {
    	if(layout_home == null) {
    		layout_home = (LinearLayout)findViewById(R.id.layout_home);
    	}
    	return layout_home;
    }
    
    private Animation getFadeIn() {
    	if(animation_fade_in == null) {
    		animation_fade_in = AnimationUtils.loadAnimation(this, R.anim.fade_in);
    	}
    	return animation_fade_in;
    }
    
    private Animation getFadeOut() {
    	if(animation_fade_out == null) {
    		animation_fade_out = AnimationUtils.loadAnimation(this, R.anim.fade_out);
    	}
    	return animation_fade_out;
    }
    
    private ImageView getLost() {
    	if(lost == null) {
    		lost = (ImageView) findViewById(R.id.imgLost);
            lost.setOnClickListener(this);
    	}
    	return lost;
    }
    
    private ImageView getFound() {
    	if(found == null) {
    		found = (ImageView) findViewById(R.id.imgFound);
            found.setOnClickListener(this);
    	}
    	return found;
    }
    
    
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        L10N.init(this.getResources());
        loadPreferences();
        setContentView(R.layout.dashboard);
        
        getLayoutHome().startAnimation(getFadeIn());
        
        getLost();
        getFound();
        drawStatistics();
    }
	
	private Intent step1;
	private Intent getStep1() {
		if(step1 == null) {
			step1 = new Intent(Dashboard.this, Step1.class); 
		}
		return step1;
	}
	
	@Override
	public void onClick(View arg0) {
		if(lost.equals(arg0)) {
			getLayoutHome().startAnimation(getFadeOut());
			TicketsService.setCurrentTicket(new Ticket(LFType.LOST));
		    startActivity(getStep1());
		} else if(found.equals(arg0)) {
			getLayoutHome().startAnimation(getFadeOut());
			TicketsService.setCurrentTicket(new Ticket(LFType.FOUND));
		    startActivity(getStep1());		
		}
	}
	
    private TextView getLblLost() {
    	if(lblLost == null) {
    		lblLost = (TextView) findViewById(R.id.lblLost);
    		lblLost.setTypeface(L10N.font);
    	}
    	return lblLost;
    }
    
    private TextView getLblFound() {
    	if(lblFound == null) {
    		lblFound = (TextView) findViewById(R.id.lblFound);
    		lblFound.setTypeface(L10N.font);
    	}
    	return lblFound;
    }
    
    private String sLost;
    private String sFound;
    private String getSLost() {
    	if(sLost == null) {
    		sLost = this.getResources().getString(R.string.lost) + " ";
    	}
    	return sLost;
    }
    private String getSFound() {
    	if(sFound == null) {
    		sFound = this.getResources().getString(R.string.found) + " ";
    	}
    	return sFound;
    }
    private void drawStatistics() {
    	getLblLost();
        getLblFound();
        
        List<String> stat = getTicketsService().count();
        if(stat!=null && stat.size()>1) {
	        lblLost.setText(getSLost() + stat.get(0) + " " + getArtLabel(stat.get(0))); 
	        lblFound.setText(getSFound() + stat.get(1) + " " + getArtLabel(stat.get(1)));
        }
    }
    
    private String getArtLabel(String count) {
    	int c = Integer.parseInt(count);
    	Resources res = Dashboard.this.getResources();
        switch(c) {
			case 1: return res.getString(R.string.artifact);
			case 2:	
			case 3:	
			case 4:	return res.getString(R.string.artifacta);
			default: return res.getString(R.string.artifacts); 	
    	}
    }
  
    @Override
    public void onResume() {
    	super.onResume();
    	if(reload) {
    		drawStatistics();
    		reload = false;
    	}
    }
}